package com.pm.controller.task;

import java.util.Date;
import java.util.List;

import com.pm.common.annotation.CurrentUser;
import com.pm.entity.po.security.User;
import com.pm.mapper.security.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.pm.common.Pages;
import com.pm.common.Response;
import com.pm.common.annotation.MyLog;
import com.pm.common.annotation.UserLoginToken;
import com.pm.common.exception.CommonException;
import com.pm.common.exception.ExpCodeState;
import com.pm.entity.po.task.Task;
import com.pm.entity.po.task.TaskCollectionCheckLog;
import com.pm.service.task.TaskCollectionCheckLogService;
import com.pm.service.task.TaskService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "重点工作审核")
@UserLoginToken
@RestController
@RequestMapping("/taskCollectionCheckLog")
public class TaskCollectionCheckLogController {
	@Autowired
	TaskCollectionCheckLogService taskCollectionCheckLogService;
	@Autowired
	TaskService taskService;
	@Autowired
	UserMapper userMapper;
	@ApiOperation("新增接口")
	@PostMapping("/add")
	@ResponseBody
	@MyLog(value = "新增审核")
	public Response<Integer> add(@RequestBody TaskCollectionCheckLog taskCollectionCheckLog) {
		Response<Integer> response = null;
		Integer result = taskCollectionCheckLogService.add(taskCollectionCheckLog);
		if (result > 0) {
			response = new Response<>(ExpCodeState.add_data_success, result);
		} else {
			response = new Response<>(ExpCodeState.add_data_failed);
		}
		return response;
	}

	@ApiOperation("更新接口")
	@PostMapping("/update")
	@ResponseBody
	@MyLog(value = "编辑审核")
	@Transactional(rollbackFor = CommonException.class)
	public Response<Integer> update(@RequestBody TaskCollectionCheckLog taskCollectionCheckLog,@CurrentUser User user) {
		Response<Integer> response = null;
		// 驳回
		if (taskCollectionCheckLog.getStatus() == -1) {
			Task task=new Task();
			task.setId(taskCollectionCheckLog.getTaskId());
			task.setCheckStatus(-1);
			taskService.update(task);
			taskCollectionCheckLog.setAuditor(user.getUserId());
			taskCollectionCheckLog.setStatus(-1);
			taskCollectionCheckLog.setRead(0);
			taskCollectionCheckLog.setTaskId(task.getId());
			Integer result = taskCollectionCheckLogService.add(taskCollectionCheckLog);
			if (result > 0) {
				response = new Response<>(ExpCodeState.update_data_success, result);
			} else {
				response = new Response<>(ExpCodeState.update_data_failed);
			}
		}
		// 通过
		if (taskCollectionCheckLog.getStatus() == 1) {
			Task task=new Task();
			task.setId(taskCollectionCheckLog.getTaskId());
			task.setCheckStatus(2);
			taskService.update(task);
			taskCollectionCheckLog.setAuditor(user.getUserId());
			Integer result = taskCollectionCheckLogService.update(taskCollectionCheckLog);
			if (result > 0) {
				response = new Response<>(ExpCodeState.update_data_success, result);
			} else {
				response = new Response<>(ExpCodeState.update_data_failed);
			}
		}
		return response;
	}

	@ApiOperation("删除接口")
	@PostMapping("/remove")
	@ResponseBody
	@MyLog(value = "删除审核")
	public Response<Integer> remove(@RequestBody TaskCollectionCheckLog taskCollectionCheckLog) {
		Response<Integer> response = null;
		Integer result = taskCollectionCheckLogService.remove(taskCollectionCheckLog);
		if (result > 0) {
			response = new Response<>(ExpCodeState.remove_data_success, result);
		} else {
			response = new Response<>(ExpCodeState.remove_data_failed);
		}
		return response;
	}

	@ApiOperation("单个对象查询接口")
	@PostMapping("/load")
	@ResponseBody
	public Response<TaskCollectionCheckLog> load(@RequestBody TaskCollectionCheckLog taskCollectionCheckLog) {
		Response<TaskCollectionCheckLog> response = null;
		TaskCollectionCheckLog result = taskCollectionCheckLogService.load(taskCollectionCheckLog);
		response = new Response<TaskCollectionCheckLog>(ExpCodeState.query_data_success, result);
		return response;
	}

	@ApiOperation("全部列表查询接口")
	@PostMapping("/getList")
	@ResponseBody
	public Response<List<TaskCollectionCheckLog>> getList(@RequestBody TaskCollectionCheckLog taskCollectionCheckLog,@CurrentUser
			User user) {
		Integer id = user.getUserId();
		Integer taskId = userMapper.getTaskId(id);
		taskCollectionCheckLog.setTaskId(taskId);
		List<TaskCollectionCheckLog> result = taskCollectionCheckLogService.getList(taskCollectionCheckLog);
		Response<List<TaskCollectionCheckLog>> res = new Response<List<TaskCollectionCheckLog>>(
				ExpCodeState.query_data_success, result);
		return res;
	}

	@ApiOperation("分页查询接口")
	@PostMapping("/getListByPage/{pageNum}/{pageSize}")
	@ResponseBody
	@MyLog(value = "查询审核")
	public Response<PageInfo<TaskCollectionCheckLog>> getList(
			@RequestBody TaskCollectionCheckLog taskCollectionCheckLog, Pages page) {
		PageInfo<TaskCollectionCheckLog> result = taskCollectionCheckLogService.getListByPage(taskCollectionCheckLog,
				page);
		Response<PageInfo<TaskCollectionCheckLog>> res = new Response<PageInfo<TaskCollectionCheckLog>>(
				ExpCodeState.query_data_success, result);
		return res;
	}

	@ApiOperation("修改打分审核记录读状态")
	@PostMapping("/updateReda")
	@ResponseBody
	@MyLog(value = "修改打分审核记录读状态")
	public Response<Integer> updateReda(TaskCollectionCheckLog taskCollectionCheckLog) {
		Response<Integer> response = null;
		//taskCollectionCheckLog.setRead(1);
		Integer result = taskCollectionCheckLogService.updateReda(taskCollectionCheckLog);
		if (result > 0) {
			response = new Response<>(ExpCodeState.update_data_success, result);
		} else {
			response = new Response<>(ExpCodeState.update_data_failed);
		}
		return response;
	}
}
